package psim;

/**
 * Tato třída zapouzdruje jednu castici - všechny její parametry
 * @author Tomas Drimal
 */
public class Particle {

    public double x; //x-ova poloha
    public double y; //y-nova poloha
    public double vx; //rychlost ve smeru osy x
    public double vy; //rychlost ve smeru osy y
    public double m; //hmotnost castice
    public double q; //naboj castice
    public double d; //prumer castice
    public short col; //barva castice
    public double old_x; //predchozi x-ova poloha
    public double old_y; //predchozi y-nova poloha
    public double old_vx; //predchozi rychlost ve smeru osy x
    public double old_vy; //predchozi rychlost ve smeru osy y

    /**
     * Konstruktor tridy particle -
     * jako parametry jsou vsechny vlastnosti tridy
     * @param c_x - xova poloho
     * @param c_y - ynov poloha
     * @param c_vx - xova rychlost
     * @param c_vy - ynova rychlost
     * @param c_m - hmotnost castice
     * @param c_q - naboj castice
     * @param c_d - prumer castice
     * @param c_col - barva castice
     */
    Particle(double c_x, double c_y, double c_vx, double c_vy,
            double c_m, double c_q, double c_d, short c_col) {
        this.x = c_x;
        this.y = c_y;
        this.vx = c_vx;
        this.vy = c_vy;
        this.m = c_m;
        this.q = c_q;
        this.d = c_d;
        this.col = c_col;

        saveVals();
    }

    /**
     * Metoda pro vypocet zmeny polohy
     * pricita k aktualni poloze hodnotu rychlosti
     * pro kazdou slozku zvlast
     */
    synchronized public void changePos() {
        x = x + (vx * Main.dt);
        y = y + (vy * Main.dt);
        if (Main.bound) {

            if (x > Main.scale_x) {
                vx = vx * (-1);
            }
            if (y > Main.scale_y) {
                vy = vy * (-1);
            }
            if (x < 0) {
                vx = vx * (-1);
            }
            if (y < 0) {
                vy = vy * (-1);
            }
        }

    }

    /**
     * Metoda, ktera preklopi nove hodnoty do starych
     */
    public void saveVals() {
        this.old_x = this.x;
        this.old_y = this.y;
        this.old_vx = this.vx;
        this.old_vy = this.vy;
    }
    /**
     * Metoda, ktera hodnoty castice prevede do retezce pro ulozeni
     * do souboru typu pts (XML)
     * @return retezec, ktery SAX serializer vlozi mezi tagy "<ptc>" a "</ptc>"
     */
    public String tString() {
        StringBuffer buff = new StringBuffer();
        buff.append(Double.toString(x) + " ");
        buff.append(Double.toString(y) + " ");
        buff.append(Double.toString(vx) + " ");
        buff.append(Double.toString(vy) + " ");
        buff.append(Double.toString(m) + " ");
        buff.append(Double.toString(q) + " ");
        buff.append(Double.toString(d) + " ");
        buff.append(Short.toString(col));
        return buff.toString();
    }
    /**
     * Metoda, ktera prevede hodnoty vlastnosti castice na
     * retezec, ktery je jednim z radku csv souboru
     * @return retezec, ktery je jednim radkem csv souboru
     */
    public String toCsvLine() {
        StringBuffer buff = new StringBuffer();
        buff.append(Double.toString(x) + ",");
        buff.append(Double.toString(y) + ",");
        buff.append(Double.toString(vx) + ",");
        buff.append(Double.toString(vy) + ",");
        buff.append(Double.toString(m) + ",");
        buff.append(Double.toString(q) + ",");
        buff.append(Double.toString(d) + ",");
        buff.append(Short.toString(col));
        return buff.toString();
    }
}
